import { routerRedux } from 'dva/router';
import { stringify } from 'qs';
import { getContactsList,saveContact,editContact,delContact } from '@/services/http';

export default {
  namespace: 'ContactList',
  state: {
    contactList: [],
    total: 0,
    isLoading: true,
    ret: {},
  },
  effects: {
    *getContactsList(payload, { call, put }) {
      const response = yield call(getContactsList, payload.payload);
      yield put({
        type: '_getContactsList',
        data: response,
      });
    },

    * saveContact(payload, {call, put}) {
      const {saveContactCallback} = payload.payload;
      const response = yield call(saveContact, payload.payload);
      saveContactCallback(response);
    },

    * editContact(payload, {call, put}) {
      const {onEditContactCallback} = payload.payload;
      const response = yield call(editContact, payload.payload);
      onEditContactCallback(response);
    },

    * delContact(payload, {call, put}) {
      const response1 = yield call(delContact, payload.payload);
      const response2 = yield call(getContactsList, payload.payload);
      yield put({
        type: '_getContactsList',
        data: response2,
      });
    },



  },

  reducers: {
    _getContactsList(state, action) {
      return {
        ...state,
        contactList: action.data.code == 1 ? action.data.ret : [],
        total: action.data.code == 1 ? action.data.total : 0,
        isLoading: false,
      };
    },
  },
};


